import os
from datetime import datetime
from enum import Enum
from typing import Dict, Optional, List, Union, Any
from pydantic import BaseModel, Field, validator
from enum import IntEnum

class MessageType(Enum):
    DEVICE_REGISTER = "device_register"
    STATUS_UPDATE = "status_update"
    DATA_REPORT = "data_report"
    AI_CHAT = "ai_chat"
    HEARTBEAT = "heartbeat"

class DeviceStatus(BaseModel):
    deviceId: str
    versionName: Optional[str] = None
    url: Optional[str] = None
    code: Optional[str] = None
    status: str = "active"
    lastSeen: Optional[datetime] = None

class WebSocketMessage(BaseModel):
    type: MessageType
    data: Dict
    timestamp: Optional[datetime] = None

class ChatMessage(BaseModel):
    role: str
    content: str
    timestamp: str


# 定义请求和响应模型
class ChatRequest(BaseModel):
    message: str
    model: str = os.getenv("DEFAULT_MODEL", "deepseek-ai/DeepSeek-R1-0528-Qwen3-8B")  # 默认模型
    max_tokens: int = 8192
    temperature: float = 0.6

class ChatResponse(BaseModel):
    response: str
    model_used: str
    tokens_used: int = None

class DeviceInfo(BaseModel):
    DeviceId: str
    IsUsed: bool
    IsOnline: bool
    DeviceModel: str
    SdkVersion: str
    last_heartbeat: Optional[datetime] = None
    ws_connection: Optional[Any] = None

class PlatformType(int, Enum):
    WECHAT = 0
    XHS = 1
    TIKTOK = 2
    KWAI = 3
    WEIBO = 4

class PlatformInfo(BaseModel):
    platform_id: str
    platform_name: str
    platform_type: PlatformType
    version: str
    description: Optional[str] = None
    api_endpoint: Optional[str] = None
    created_at: Optional[datetime] = None

class DevicePlatformBinding(BaseModel):
    binding_id: str
    device_id: str
    platform_id: str
    bind_time: datetime
    unbind_time: Optional[datetime] = None
    is_active: bool = True
    bind_config: Optional[Dict[str, Any]] = None

class BindRequest(BaseModel):
    device_id: str
    platform_id: str
    bind_config: Optional[Dict[str, Any]] = None

class UnbindRequest(BaseModel):
    device_id: str
    platform_id: str
    reason: Optional[str] = None

class PlatformDataDetail(BaseModel):
    """平台数据详情"""
    platform_type: PlatformType = Field(..., description="平台类型")
    data_type: int = Field(-1, description="数据类型")
    data: Optional[Dict[str, Any]] = Field(None, description="数据内容")
    timestamp: Optional[datetime] = Field(default_factory=datetime.now, description="数据时间戳")
    version: str = Field("1.0", description="数据格式版本")
    metadata: Optional[Dict[str, Any]] = Field(None, description="元数据")
    data_id: Optional[str] = Field(None, description="数据唯一ID")

class PlatformDataType(IntEnum):
    """平台数据类型"""
    UNKNOWN = 0
    MESSAGE = 1           # 消息数据
    USER_INFO = 2         # 用户信息
    CONTENT = 3           # 内容数据
    INTERACTION = 4       # 互动数据
    TASK_RESULT = 5       # 任务结果
    STATISTICS = 6        # 统计信息
    COMMAND = 7           # 命令数据
    CONFIG = 8            # 配置信息
    MEDIA = 9             # 媒体文件
    NOTIFICATION = 10     # 通知信息